/**
 * 
 */
package com.flux.tms.tm${bean.numMain}web.modules.t${bean.numMain}${bean.numSub};

import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.flux.tms.tm${bean.numMain}api.${bean.javaFlieName}Service;

import com.flux.scev6.appsecret.AppSecretService;
import com.flux.scev6.constant.CommonCVal;
import com.flux.scev6.dbopr.model.ResultObj;
import com.flux.scev6.function.functionhelper.SFHelper;
import com.flux.scev6.importbiz.impfmfile.DefaultImpFmFileHelper;
import com.flux.scev6.language.multilang.MultiLangHandler;
import com.flux.scev6.login.LoginInfoUtil;
import com.flux.scev6.login.model.LoginInfoObj;
import com.flux.scev6.rpc.hessian.api.IHessianClient;
import com.flux.scev6.rpc.rpc.RemoteServiceUtil;
import com.flux.scev6.utils.BizDataUtil;
import com.flux.scev6.utils.ResultObjUtil;


/**
 * 
 * 辅助类 -- 从Excel文件中导入数据时的辅助类
 *
 * <p>
 * 创建时间：${bean.dayTime}
 * <p>
 *
 * <p>
 * 创建时间：${bean.dayTime}
 * <p>
 *
 * @author ${bean.author}
 *
 * @version v1.0.0
 */
public class ImpHeaderFmExcelHelper extends DefaultImpFmFileHelper {
    /**
     * V100(${bean.numTime}) ${bean.author} 初版。 
    */
    @SuppressWarnings("unused")
    private static String verNum = "V100(${bean.numTime})";// 版本号
    
   // @Autowired(required=false)
	//private ${bean.javaFlieName}Service bizSystemCodeTypeServiceProvider;
    
	public ImpHeaderFmExcelHelper(HttpServletRequest request, LoginInfoObj loginInfoObj, String fid, String tableId) {
	    this.request = request;
	    this.strSFID = fid;//系统功能ID
	    this.strTABID = tableId;//表格ID
		this.loginInfoObj = loginInfoObj;
	}
	
	@Override
	public ResultObj handleOneRowData(Map<String, String> bizdataMap) {
		// 调用服务，保存数据
        // 根据当前登录信息对象构造多语言处理器
        MultiLangHandler mlh = new MultiLangHandler(request, loginInfoObj);
	    // 当前用户登录的企业组织编号 // 注意：极其重要！
        bizdataMap.put(${bean.javaFlieName}CVal.KEYNAME_ORGANIZATIONID, this.loginInfoObj.getBizOrgId());
        // 获取服务提供类
        AppSecretService appSecretService;
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
        appSecretService = (AppSecretService) ctx.getBean("appSecretService");
        
        RemoteServiceUtil remoteServiceUtil;
        ctx = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
        remoteServiceUtil = (RemoteServiceUtil) ctx.getBean("remoteServiceUtil");
        
        IHessianClient hessianClient;
        ctx = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
        hessianClient = (IHessianClient) ctx.getBean("hessianClient");
        
        String appSecret = appSecretService.getAppSecret();
        String loginInfo = LoginInfoUtil.toJSONString(this.loginInfoObj, appSecret);
        // 设置导入标识，新增查询此数据存在时后面返回判断使用
        String bizData = BizDataUtil.toJSONString(bizdataMap, appSecret);
        String srvResult = null;
        // ==============================================================================
        // ==============================================================================
        // ==============================================================================
        if (remoteServiceUtil.isHessian()) {
            // 通过Hessian方式调用后端服务类程序
            String clsId = "${bean.javaFlieName}ServiceProvider";
            String method = null;
            String version = "1.0.0";
            method = "add${bean.javaFlieName}";
            srvResult = hessianClient.doAction(clsId, method, version, loginInfo, bizData);
        }
    //    else if (bizSystemCodeTypeServiceProvider != null) {
            // 通过HSF方式调用后端服务类程序
            // 调用对应的后端服务类程序
//            srvResult = bizSystemCodeTypeServiceProvider.addCodeType(loginInfo, bizData);
   //     }
        // ==============================================================================
        // ==============================================================================
        // ==============================================================================
        // 处理服务类返回的结果信息
        ResultObj resultObj = ResultObjUtil.fromJSONString(srvResult, appSecret);
        
        if (!resultObj.isCodeValueOK()) {
            // 错误处理 //把消息Id翻译成当前用户使用语言对应的文本描述
            //ACT_IMP = "ACT_IMP";    // 标准导入标识
            String tableName = strTABID;
            if(resultObj.getMessageId().equalsIgnoreCase(CommonCVal.ACT_IMP)){
                bizdataMap.put(CommonCVal.KEYNAME_OPRSEQFLAG, resultObj.getOprSeqFlag01());
                // 执行更新操作
                resultObj = updateData(remoteServiceUtil, hessianClient, loginInfo, appSecret,bizdataMap);
                if(!resultObj.isCodeValueOK()){
                    String msg = mlh.getServiceMessage(tableName, resultObj.getMessageId(), resultObj.getMessage());
                    resultObj.setMessage(msg);
                };
            }else{
                String msg = mlh.getServiceMessage(tableName, resultObj.getMessageId(), resultObj.getMessage());
                resultObj.setMessage(msg);
            };
        }
		return resultObj;
	};
	/**
	 *  更新数据
	 *
	 * @author ${bean.author}  
	 *
	 * @date ${bean.dayTime}
	 * 
	 * @param remoteServiceUtil
	 * @param hessianClient
	 * @param loginInfo
	 * @param appSecret
	 * @param bizdataMap
	 * @return
	 */
    private ResultObj updateData(RemoteServiceUtil remoteServiceUtil, IHessianClient hessianClient, String loginInfo, String appSecret,Map<String, String> bizdataMap) {
        // 返回结果
        ResultObj result = new ResultObj();
        String srvResult = null;
        // 当前用户登录的企业组织编号 // 注意：极其重要！
        bizdataMap.put(${bean.javaFlieName}CVal.KEYNAME_ORGANIZATIONID, this.loginInfoObj.getBizOrgId());
        bizdataMap.put(CommonCVal.OPRFLAG, CommonCVal.OPRFLAG_EDIT);
        // ==============================================================================
        // 更新导入时，不做为空的字段导入处理
        clearEmptyColumns(bizdataMap);
        // ==============================================================================
        String bizData   = BizDataUtil.toJSONString(bizdataMap, appSecret);
        // ==============================================================================
        // ==============================================================================
        if (remoteServiceUtil.isHessian()) {
            // 通过Hessian方式调用后端服务类程序
            String clsId = "${bean.javaFlieName}ServiceProvider";
            String method = null;
            String version = "1.0.0";
            method = "update${bean.javaFlieName}";
            srvResult = hessianClient.doAction(clsId, method, version, loginInfo, bizData);
        }
    //  else if (bizSystemCodeTypeServiceProvider != null) {
            // 通过HSF方式调用后端服务类程序
            // 调用对应的后端服务类程序
//            srvResult = bizSystemCodeTypeServiceProvider.updateCodeType(loginInfo, bizData);
       // }
        // ==============================================================================
        // ==============================================================================
        // ==============================================================================
        // 处理服务类返回的结果信息
        result = ResultObjUtil.fromJSONString(srvResult, appSecret);
        
        return result;
    }
    // 处理复制信息中map为空的字段进行clear
    private void clearEmptyColumns(Map<String, String> bizdataMap){
        Iterator<Map.Entry<String, String>> it = bizdataMap.entrySet().iterator();  
        while (it.hasNext()) {  
            Map.Entry<String, String> entry = it.next(); 
            String key = entry.getKey();
            String value = entry.getValue();
            if(!SFHelper.isNotNull(value) && !value.equalsIgnoreCase("")){
                bizdataMap.remove(key);
            };
        }
    };
}
